/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.ListSubjectOpenDAL;
import mnstudent.datatranferobject.ListSubjectOpenDTO;
import mnstudent.datatranferobject.SubjectDisplay;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXTable;
/**
 *
 * @author lee
 */
public class ListSubjectOpenBLL {
   ListSubjectOpenDAL listSubjectOpenBLL;
    
    public ListSubjectOpenBLL()
    {
        listSubjectOpenBLL = new ListSubjectOpenDAL();
    }
    
    public ResultSet getAllSubjectWithKindOfSubject()
    {
        return listSubjectOpenBLL.getAllSubjectWithKindOfSubject();
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getAllListSubjectOpenBySemesterYear(String _idYear, String _idSemester)
    {
        return listSubjectOpenBLL.getAllListSubjectOpenBySemesterYear(_idYear, _idSemester);
    }
    
    public ArrayList<SubjectDisplay> getAllListSubjectOpenBySemesterYearToArray(String _idYear, String _idSemester) {
        ArrayList<SubjectDisplay> arrListSubjectOpen = new ArrayList<>();
        try {
            ResultSet rs = getAllListSubjectOpenBySemesterYear(_idYear, _idSemester);
            while (rs.next()) {
                SubjectDisplay listSubjectOpen = new SubjectDisplay(rs.getNString(1), rs.getNString(2), rs.getNString(3), rs.getInt(4));
                arrListSubjectOpen.add(listSubjectOpen);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ListSubjectOpenBLL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return arrListSubjectOpen;
    }
    /**
     * ham them moi khoa xuong database
     * @param _listSubjectOpen
     * @return true: success, false: fail
     */
    public boolean insertListSubjectOpenToDatabase(ListSubjectOpenDTO _listSubjectOpen)
    {
        return listSubjectOpenBLL.insertListSubjectOpenToDatabase(_listSubjectOpen);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _listSubjectOpen
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateListSubjectOpenToDatabase(ListSubjectOpenDTO _listSubjectOpen)
    {
          return listSubjectOpenBLL.updateListSubjectOpenToDatabase(_listSubjectOpen);
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _listSubjectOpen
     * @return 
     */
     public boolean deleteListSubjectOpenFromDatabase(ListSubjectOpenDTO _listSubjectOpen)
    {
        return listSubjectOpenBLL.deleteListSubjectOpenFromDatabase(_listSubjectOpen);
    }
     
    public boolean deleteListSubjectOpenBySemesterYearFromDatabase(String _idYear, String _idSemester)
    {
        return listSubjectOpenBLL.deleteListSubjectOpenBySemesterYearFromDatabase(_idYear, _idSemester);
    }
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
}
